{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# 利用数组进行数据处理\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-5 -3 -1  1  3]\n",
      " [-5 -3 -1  1  3]\n",
      " [-5 -3 -1  1  3]\n",
      " [-5 -3 -1  1  3]\n",
      " [-5 -3 -1  1  3]]\n",
      "[[-5 -5 -5 -5 -5]\n",
      " [-3 -3 -3 -3 -3]\n",
      " [-1 -1 -1 -1 -1]\n",
      " [ 1  1  1  1  1]\n",
      " [ 3  3  3  3  3]]\n"
     ]
    }
   ],
   "source": [
    "points = np.arange(-5, 5, 2) # 1000个间隔相等的点\n",
    "xs, ys = np.meshgrid(points, points) # 产生两个二维矩阵\n",
    "print(xs) # xs每一行对应points\n",
    "print(ys) # ys每一列对应points"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 7.07106781,  5.83095189,  5.09901951,  5.09901951,  5.83095189],\n",
       "       [ 5.83095189,  4.24264069,  3.16227766,  3.16227766,  4.24264069],\n",
       "       [ 5.09901951,  3.16227766,  1.41421356,  1.41421356,  3.16227766],\n",
       "       [ 5.09901951,  3.16227766,  1.41421356,  1.41421356,  3.16227766],\n",
       "       [ 5.83095189,  4.24264069,  3.16227766,  3.16227766,  4.24264069]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z = np.sqrt(xs ** 2 + ys ** 2)\n",
    "z"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.colorbar.Colorbar at 0x1d41a69a0b8>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAASQAAAEPCAYAAADrkrkZAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAFaZJREFUeJzt3X+wHWV9x/H3hxgShGCqiQaSSGKxmSrWRG9TOihqUBsh\nQEVbQcGKdlKnolBpqdRW4oxO/dEy2KnViSBYBfxBQJDBYCzJUCwJJOFKSQIdJMEkRpNbGwyhDSb5\n9o/dy5x7zz2/99599ubzmjmTs2ef3ee7mz3f++zz7O5RRGBmloKjyg7AzGyQE5KZJcMJycyS4YRk\nZslwQjKzZDghmVkynJDMLBnPKzsAG0rS5cA/lB1HNyJCZcdg1eaElJ7p/mLbkcqnbAmRNBv4adlx\nmJXFCSkt5wK3t1NQ0kJJ90u6V9LNkiaOZmBjXZ8dmY6YhCRpm6Q3j0E98yT1S9on6SMdLj4rIna2\nWXY7sCgiTge2kSWz0dRRfT3uhzEnaZOkNzaZf4OkT3Wx3lHbD2N1TI+ltvqQJG0D/jQifji64ZSv\ngG29AlgdEfM7rPdFwEC75SNiV83ks8DhTurrVBf1dbUfyhIRrxylVVdqP5TtiGkhjaGTgE1dLHc2\n8L1OF5J0EvDWVstKWiZpWRdxdVUf3e8HJI3ZYMsY1NX1fjgSdZyQ8mbiX0l6WNJ+SddJeomk7+fN\n0h9K+o2a8h+T9JN83mZJb6+Z9xpJD+XzviPpW7XNYkknSlohaY+krc2avHlcV+Z1/I+k6yVNblD2\ntyWtkbQ3b6qfk3/+deClwPckPS3pig6Xvwd4E/DP+fK/1cGunRcRjw2r53OSvlsz/XlJ/ybp6Hz6\neODrwPsi4tcd1DUiScdJOiTphJrPTpG0S9KUdutrtB8a7bd83jZJfy3pYWD/SImi2bE0QtmGx9ZI\ndQ0//ZG0QNLGfPlvASMeS822q9XxkMdwy7DPviDp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Mi9gKrSbuv7jTgHEnbyLoZFkn6RrkhpaPKCelB4OWS5ko6GjgfuKPkmMYFSQKuA7ZExNVl\nx9OMpOmSpubvjwHeAjxablSNRcSVETErIuaQHbP3RMSFJYeVjMompIg4CFwC3E3W6frtiNhUblSN\nSboZuB+YJ2mHpA+UHVMTpwEXkf317s9fZ5YdVAMnAKslPUz2R2pVRHgovaJ864iZJaOyLSQzG3+c\nkMwsGU5IZpYMJyQzS4YTkpklwwnJzJLhhGRmyfh/jIgynQvty8IAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x1d41a63ef98>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.title(\"Image plot of $\\sqrt{x^2 + y^2}$ for a grid of values\")\n",
    "plt.imshow(z, cmap=plt.cm.gray); plt.colorbar() # 用颜色代表x/y轴对应点的值，以后学matplotlib再详细讲解。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 将条件逻辑表述为数组运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1.1000000000000001, 2.2000000000000002, 1.3, 1.3999999999999999, 2.5]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xarr = np.array([1.1, 1.2, 1.3, 1.4, 1.5])\n",
    "yarr = np.array([2.1, 2.2, 2.3, 2.4, 2.5])\n",
    "cond = np.array([True, False, True, True, False])\n",
    "result = [(x if c else y) for x, y, c in zip(xarr, yarr, cond)] # 根据cond决定取值\n",
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1.1,  2.2,  1.3,  1.4,  2.5])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result = np.where(cond, xarr, yarr) # 前面的做法只适用于一维数组，where则更通用。\n",
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2, -2,  2, -2],\n",
       "       [-2,  2,  2,  2],\n",
       "       [-2, -2,  2, -2],\n",
       "       [-2,  2, -2, -2]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.random.randn(4, 4)\n",
    "np.where(arr > 0, 2, -2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2.        , -2.42433276,  2.        , -0.05655187],\n",
       "       [-1.05119426,  2.        ,  2.        ,  2.        ],\n",
       "       [-0.19063612, -0.2075412 ,  2.        , -0.40575162],\n",
       "       [-0.81511827,  2.        , -1.00482085, -0.29014986]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.where(arr > 0, 2, arr) # 只对正值处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 2, 3, 1])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cond1 = np.array([True, False, False, True])\n",
    "cond2 = np.array([True, True, False, False])\n",
    "np.where(cond1 & cond2,\n",
    "         0,\n",
    "         np.where(cond1,\n",
    "                  1,\n",
    "                  np.where(cond2, 2, 3)))\n",
    "# 从最内层where展开，如果cond2为True，取2，否则取3。\n",
    "# 对于cond1，如果cond1为True，取1。否则cond1为False，cond2为True取2，都为False取3。\n",
    "# 继续叠加。。。\n",
    "# 建议先写if-else再用where优化。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 数学和统计方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-0.60642898 -0.71536577  1.62634848 -1.53876013]\n",
      " [ 1.59465797 -1.69712896  0.64345977  0.02692956]\n",
      " [-1.05746591  0.19955349  0.53481112  0.84087373]\n",
      " [ 1.35288362 -0.53339505 -0.25475596  1.96991622]\n",
      " [-0.75848892 -1.23247927  0.29068937  1.57295488]]\n",
      "0.112940462058\n",
      "0.112940462058\n",
      "2.25880924115\n",
      "[-0.3085516   0.14197958  0.12944311  0.63366221 -0.03183099]\n",
      "[ 0.52515777 -3.97881557  2.84055279  2.87191425]\n"
     ]
    }
   ],
   "source": [
    "arr = np.random.randn(5, 4) # 正态分布的数据\n",
    "print(arr)\n",
    "print(arr.mean())\n",
    "print(np.mean(arr))\n",
    "print(arr.sum()) # 所有数字加起来\n",
    "print(arr.mean(axis=1)) # 对每一行求平均\n",
    "print(arr.sum(0)) # 对每一列求和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2]\n",
      " [ 3  5  7]\n",
      " [ 9 12 15]]\n",
      "[[  0   0   0]\n",
      " [  3  12  60]\n",
      " [  6  42 336]]\n"
     ]
    }
   ],
   "source": [
    "arr = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])\n",
    "print(arr.cumsum(0)) # 对每一列累加\n",
    "print(arr.cumprod(1)) # 对每一行累乘"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 基本数组统计方法\n",
    "# sum\n",
    "# mean\n",
    "# std/var：      标准差和方差\n",
    "# min/max\n",
    "# argmin/argmax：分别为最小和最大元素的索引\n",
    "# cumsum\n",
    "# cumprod"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 用于布尔型数组的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "49\n",
      "51\n"
     ]
    }
   ],
   "source": [
    "arr = np.random.randn(100)\n",
    "print((arr > 0).sum()) # 正数的数量\n",
    "print((arr <= 0).sum())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "bools = np.array([False, False, True, False])\n",
    "print(bools.any()) # 是否有True（对于非布尔数组，非0就是True）\n",
    "print(bools.all()) # 是否全部为True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-0.41053948, -0.33567028, -0.14980025,  0.12637499,  1.22385731,\n",
       "        1.55645311,  2.50815208,  2.58220013])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.random.randn(8)\n",
    "arr.sort()\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.75839427, -0.59546501, -0.31912824],\n",
       "       [-1.21207972, -1.10972785, -0.59829973],\n",
       "       [-1.13229569, -0.07776177,  0.27082918],\n",
       "       [-2.33452632, -0.20187988,  0.6254117 ],\n",
       "       [-0.85581661,  0.11031514,  1.75206503]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.random.randn(5, 3)\n",
    "arr.sort(1) # 对每行排序\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1.6516510631638368"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "large_arr = np.random.randn(1000)\n",
    "large_arr.sort()\n",
    "large_arr[int(0.05 * len(large_arr))] # 5%分位数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 唯一化以及其他的集合逻辑"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Bob', 'Joe', 'Will']\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array(['Bob', 'Joe', 'Will'],\n",
       "      dtype='<U4')"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names = np.array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'])\n",
    "print(sorted(set(names))) # 纯Python做法)\n",
    "np.unique(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ints = np.array([3, 3, 3, 2, 2, 1, 1, 4, 4])\n",
    "np.unique(ints)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True, False, False,  True,  True, False,  True], dtype=bool)"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "values = np.array([6, 0, 0, 3, 2, 5, 6])\n",
    "np.in1d(values, [2, 3, 6]) # 元素是否属于后面一个数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 数组的集合运算\n",
    "# unique\n",
    "# intersect1d：\n",
    "# union1d\n",
    "# in1d：       得到一个表示“x的元素是否包含于y”的布尔型数组\n",
    "# setdiff1d\n",
    "# setxor1d"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
